package com.icebartech.base.modules.common.service.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icebartech.base.modules.common.service.SmsSendService;
import com.icebartech.core.conf.SmsProperties;
import com.icebartech.core.utils.HttpConnectUtil;

@Service
public class SmsSendServiceImpl implements SmsSendService {
	
	private static Logger logger = LoggerFactory.getLogger(SmsSendServiceImpl.class);
	
	@Autowired
	private SmsProperties smsProperties;

	@Override
	public boolean sendSmsByMobile(String mobile, String vars, String projectCode) {
		boolean flag = false;
		Map<String, Object> paramMap = new LinkedHashMap<>();
		paramMap.put("appid", smsProperties.getAppId());
		paramMap.put("project", projectCode);
		paramMap.put("to", mobile);
		paramMap.put("vars", vars);
		paramMap.put("signature", smsProperties.getAppKey());
		String resultStr = HttpConnectUtil.doPost(smsProperties.getUrl(), paramMap, "UTF-8");
		if(StringUtils.isNotEmpty(resultStr)){
			JSONObject result = JSON.parseObject(resultStr);
			if("success".equals(result.get("status"))){
				logger.info("sendSmsByMobile success, mobile:{}, vars:{}, projectCode:{}", new Object[]{mobile, vars, projectCode});
				flag = true;
			}else{
				logger.error("sendSmsByMobile fail, mobile:{}, vars:{}, projectCode:{}, code:{}", new Object[]{mobile, vars, projectCode, resultStr});
			}
		}
		return flag;
	}

}
